package org.jflame.context.security.satoken;

import org.jflame.commons.util.ArrayHelper;
import org.jflame.commons.util.CollectionHelper;
import org.jflame.context.security.SecurityService;

import java.util.Collection;

import cn.dev33.satoken.stp.StpUtil;

public class SaTokenSecurityService implements SecurityService {

    @Override
    public Object getPrincipal() {
        return StpUtil.getLoginId();
    }

    @Override
    public boolean hasAuthority(String authority) {
        return StpUtil.hasPermission(authority);
    }

    @Override
    public boolean hasAnyAuthority(String... authority) {
        return StpUtil.hasPermissionOr(authority);
    }

    @Override
    public boolean hasRole(String role) {
        return StpUtil.hasRole(role);
    }

    @Override
    public boolean hasAnyRole(String... role) {
        return StpUtil.hasRoleOr(role);
    }

    @Override
    public boolean hasAllRoles(Collection<String> roleSet) {
        if (CollectionHelper.isEmpty(roleSet)) {
            return false;
        }
        return StpUtil.hasRoleAnd(ArrayHelper.toArray(roleSet));
    }

}
